package cn.ciis.sys.menu;

import cn.ciis.basic.auth.AdminAuthService;
import cn.ciis.basic.base.BaseController;
import cn.ciis.basic.model.Menu;
import cn.ciis.sys.permission.Notes;
import cn.ciis.sys.permission.Remark;
import cn.ciis.sys.taxonomy.TaxonomyAdminService;
import com.jfinal.aop.Aop;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 冉志林
 */
public class MenuAdminController extends BaseController {

    @Inject
    MenuAdminService srv;

    @Inject
    TaxonomyAdminService taxonomyAdminService;

    private AdminAuthService adminAuthSrv = Aop.get(AdminAuthService.class);

    @Notes(value = "菜单管理")
    @Remark(value = "菜单列表")
    public void index() {
        set("taxonomyList", srv.taxonomyList());
        set("taxonomyList", taxonomyAdminService.getTaxonomyList());
        kIndex();
    }

    @Remark(value = "根据所属项目获取菜单列表")
    public void findByTaxonomyMenu(Integer id) {
        if (null != id) {
            kSuccess(srv.findByTaxMenu(id));
        }
    }

    @Remark(value = "菜单保存或修改")
    @Before(MenuAdminValidator.class)
    public void saveOrUpdate() {
        Menu bean = getBean(Menu.class);
        Integer state = getParaToInt("menu.state1");
        if (state == 0) {
            // 模块
            bean.setState(0);
            bean.setSuperior(-1);
        }
        renderJson(srv.saveOrUpdate(bean));
    }

    @Remark(value = "菜单删除")
    public void deletes(String ids) {
        renderJson(srv.deletes(ids));
    }

    @Remark(value = "菜单切换")
    public void menuSwitch(Integer id) {
        List<Record> taxMenu = srv.findByMenu(id);
        renderJson(srv.groutBySuperior(taxMenu, getLoginAccountId()));
    }

    @Remark(value = "获取系统模块信息")
    public void findMenuModel(String taxId) {
        if (null != taxId) {
            Map<String, Object> map;
            map = new HashMap<>();
            map.put("taxId", taxId);
            map.put("state", 0);
            map.put("status", 1);
            Model<?> taxonomy = taxonomyAdminService.findById(taxId);
            List<?> byFiled = srv.findByFiled(map, "serialNumber");
            boolean superAdmin = adminAuthSrv.isSuperAdmin(getLoginAccountId());
            if (!superAdmin) {
                for (int i = 0; i < byFiled.size(); i++) {
                    Integer id = ((Menu) byFiled.get(i)).getInt("id");
                    List<Record> byMenu = srv.findByMenu(id);
                    int a = 0;
                    for (Record menu : byMenu) {
                        String url = menu.getStr("url");
                        if (StrKit.notBlank(url) && !"/".equals(url)) {
                            boolean b = adminAuthSrv.hasPermission(getLoginAccountId(), url);
                            if (b) {
                                a++;
                            }
                        }
                    }
                    if (a == 0) {
                        byFiled.remove(i);
                        i--;
                    }
                }
            }
            kSuccess(Ret.ok().set("data", byFiled).set("taxonomy", taxonomy));
        }
    }

    @Remark(value = "获取菜单列表")
    public void findByModelMenu() {
        renderJson(srv.findByMenu(getParaToInt("id"), 0));
    }

    @Remark(value = "查询模块的最大编号")
    public void findMaxModelSerial(String taxId) {
        Menu maxSerial = srv.findMaxSerial(taxId);
        
        renderJson(maxSerial);
    }

    @Remark(value = "查询菜单的最大编号")
    public void findMaxMenuSerial(String taxId, String modelId, String menuId) {
        renderJson(srv.findMaxMenuSerial(taxId, modelId, menuId));
    }

    @Remark(value = "查询系统下的模块信息")
    public void findModel(String taxId) {
        kSuccess(srv.findModel(taxId));
    }

    @Remark(value = "查询系统模块下菜单信息")
    public void findMenu(String taxId, String modelId) {
        kSuccess(srv.findMenu(taxId, modelId));
    }


}
